package cn.edu.lingnan.controller;


import cn.edu.lingnan.Listener.StudentsListener;
import cn.edu.lingnan.entity.excelPojo.StudentContest;
import cn.edu.lingnan.mapper.StudentContestMapper;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.analysis.ExcelReadExecutor;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/Excel")
public class ExcelController {

    @Autowired
    StudentsListener studentsListener;

    @Autowired
    StudentContestMapper studentContestMapper;


    @PostMapping("/read")
    public String readExcel(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
//      获取工作蒲
        ExcelReader excelReader = EasyExcel.read(inputStream, StudentContest.class, studentsListener).build();
//      工作表
        // 获取 SheetExecutor 对象
        ExcelReadExecutor excelReadExecutor = excelReader.excelExecutor();
//        获取所有工作表
        List<ReadSheet> readSheets = excelReadExecutor.sheetList();

        for (ReadSheet readSheet : readSheets) {

            readSheet.setHeadRowNumber(3);//设置读取第几行


            excelReader.read(readSheet);


        }

        excelReader.finish();


        return "success";
    }

    @GetMapping("/writeById")
    public void writeExcelById(@RequestParam("data") List<Integer> ids, HttpServletResponse response) throws IOException {


        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment");

//        System.out.println(arrayList);
        ArrayList<StudentContest> temp = new ArrayList<>();


        for (Integer id :
                ids) {

            StudentContest studentContest = studentContestMapper.selectById(id);
            temp.add(studentContest);
            System.out.println(studentContest);
        }
        //数据获取到集合后获取输出流
        EasyExcel.write(response.getOutputStream(), StudentContest.class)
                .sheet()
                .doWrite(temp);


    }

    @GetMapping("/writeall")
    public void writeall(@RequestParam("contest") String name, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment");
        LambdaQueryWrapper<StudentContest> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (name.equals("无")) {
            List<StudentContest> studentContests = studentContestMapper.selectList(lambdaQueryWrapper);
            //数据获取到集合后获取输出流
            EasyExcel.write(response.getOutputStream(), StudentContest.class)
                    .sheet()
                    .doWrite(studentContests);
        } else {
            lambdaQueryWrapper.eq(StudentContest::getContestName, name);
            List<StudentContest> studentContests = studentContestMapper.selectList(lambdaQueryWrapper);
            EasyExcel.write(response.getOutputStream(), StudentContest.class)
                    .sheet()
                    .doWrite(studentContests);

        }


    }


}
